package cn.stylefeng.guns.modular.yingbao.service.impl;
import java.util.Arrays;
import	java.util.HashMap;
import	java.util.Map;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysBuilding;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.entity.SysUserGroup;
import cn.stylefeng.guns.modular.yingbao.mapper.SysUserGroupMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysUserGroupParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysUserGroupResult;
import cn.stylefeng.guns.modular.yingbao.service.SysBuildingService;
import  cn.stylefeng.guns.modular.yingbao.service.SysUserGroupService;
import cn.stylefeng.guns.modular.yingbao.service.SysUserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户团队表 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-18
 */
@Service
public class SysUserGroupServiceImpl extends ServiceImpl<SysUserGroupMapper, SysUserGroup> implements SysUserGroupService {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysBuildingService sysBuildingService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysUserGroupParam param){
        SysUserGroup entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 360)
    public void delete(SysUserGroupParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class,timeout = 360)
    public void update(SysUserGroupParam param){
        SysUserGroup oldEntity = getOldEntity(param);
        SysUserGroup newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysUserGroupResult findBySpec(SysUserGroupParam param){
        return null;
    }

    @Override
    public List<SysUserGroupResult> findListBySpec(SysUserGroupParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysUserGroupParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData groupList(Long userid,int pageNum, int pageSize) {
        if (userid==null ){
            return ResponseData.error("参数错误！");
        }
        String idList = baseMapper.findId(String.valueOf(userid));
        String replace = idList.replace("$,", "");
        String replace1 = replace.replace(String.valueOf(userid) + ",", "");
        String[] split = replace1.split(",");

        List<String> list = Arrays.asList(split);  //此集合无法操作添加元素
        System.out.println(list);
        if (list.size()==1){
            if ( list.get(0).equals(String.valueOf(userid))){
                return  ResponseData.error("暂无数据");
            }
        }
        IPage<SysUserGroup> sysBuildingIPage = baseMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<SysUserGroup>().in("userid",list));

        return sysBuildingIPage.getTotal() > 0 ?ResponseData.success(200,"成功",sysBuildingIPage):ResponseData.error("暂无数据");

    }

    @Override
    public ResponseData groupyjList(Long userid, int pageNum, int pageSize) {
        if (userid==null ){
            return ResponseData.error("参数错误！");
        }
        IPage<SysUserGroup> sysBuildingIPage = baseMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<SysUserGroup>()
                .eq("referrer_id", userid));

        return sysBuildingIPage.getTotal() > 0 ?ResponseData.success(200,"成功",sysBuildingIPage):ResponseData.error("暂无数据");

    }

    @Override
    public ResponseData groupejList(Long userid, int pageNum, int pageSize) {
        if (userid==null ){
            return ResponseData.error("参数错误！");
        }
        IPage<SysUserGroup> sysBuildingIPage = baseMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<SysUserGroup>().eq("sj_referrer_id", userid));

        return sysBuildingIPage.getTotal() > 0 ?ResponseData.success(200,"成功",sysBuildingIPage):ResponseData.error("暂无数据");

    }

    @Override
    public ResponseData groupeSum(Long userid) {
        if (userid==null ){
            return ResponseData.error("参数错误！");
        }
        Map<String, Integer> map = new HashMap<> ();
        int yj = baseMapper.selectCount(new QueryWrapper<SysUserGroup>().eq("referrer_id", userid));
        int ej = baseMapper.selectCount(new QueryWrapper<SysUserGroup>().eq("sj_referrer_id", userid));

        String idList = baseMapper.findId(String.valueOf(userid));
        String replace = idList.replace("$,", "");
        String replace1 = replace.replace(String.valueOf(userid) + ",", "");
        String[] split = replace1.split(",");
        //此集合无法操作添加元素
        List<String> list = Arrays.asList(split);
        int yx=0;
        if (list!=null&&yj!=0){
            yx = sysBuildingService.count(new QueryWrapper<SysBuilding>().in("userid",list).eq("stuats","1"));
        }
        map.put("yj",yj);
        map.put("ej",ej);
        map.put("yx",yx);
        return ResponseData.success(200,"成功",map);
    }

    @Override
    public ResponseData sreachID(Long id,Long userid) {
        if (id==null ){
            return ResponseData.error("参数错误！");
        }
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("referrer_code", id));
        if (sysUser==null){
            return ResponseData.error("暂无数据！");
        }

        IPage<SysUserGroup> sysUserGroup = baseMapper.selectPage( new Page<>(0, 999), new QueryWrapper<SysUserGroup>().eq("userid",sysUser.getId())
        .and(i ->i.eq("referrer_id", userid).or().eq("sj_referrer_id",userid)));
        return sysUserGroup.getTotal() > 0 ?ResponseData.success(200,"成功",sysUserGroup):ResponseData.error("暂无数据");
    }

    @Override
    public List<String> findDescId(String id) {

        if (StringUtils.isEmpty(id) ){
            return null;
        }
        String idList = baseMapper.findDescId(String.valueOf(id));
        String replace = idList.replace("$,", "");
        String replace1 = replace.replace(id + ",", "");
        String replace2 = replace1.replace( ",0", "");
        String[] split = replace2.split(",");
        //此集合无法操作添加元素
        List<String> list = Arrays.asList(split);
        return list;
    }

    @Override
    public List<String> findAscId(Long userid){

        String idList = baseMapper.findId(String.valueOf(userid));
        String replace = idList.replace("$,", "");
        String replace1 = replace.replace(String.valueOf(userid) + ",", "");
        String[] split = replace1.split(",");

        List<String> list = Arrays.asList(split);  //此集合无法操作添加元素
        return list;
    }

    private Serializable getKey(SysUserGroupParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysUserGroup getOldEntity(SysUserGroupParam param) {
        return this.getById(getKey(param));
    }

    private SysUserGroup getEntity(SysUserGroupParam param) {
        SysUserGroup entity = new SysUserGroup();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
